axios.defaults.baseURL = 'http://api-breakingnews-web.itheima.net';


axios.interceptors.request.use(function (config) {
    // console.log(config);
    // 判断路径，包含/my或者/my开头的路径添加身份
    if (config.url.indexOf('/my') !== -1) {
        // 不要用对象覆盖，原有设置会被覆盖掉
        config.headers['Authorization'] = localStorage.getItem('token')
    }
    return config
}, function (error) {
    // 若请求错误
    return Promise.reject(error)
});

axios.interceptors.response.use(function (response) {
    // console.log(response);
    if (response.data.message === '身份认证失败！') {
        location.href = '/login.html';
        localStorage.removeItem('token');

    }
    return response
}, function (error) {
    return Promise.reject(error)
});

function formatData(time) {
    let dt = new Date(time);
    let y = dt.getFullYear()
    let m = padZero(dt.getMonth() + 1)
    let d = padZero(dt.getDate());

    let hh = padZero(dt.getHours());
    let mm = padZero(dt.getMinutes());
    let ss = padZero(dt.getSeconds());

    return `${y}-${m}-${d} ${hh}:${mm}:${ss}`
}
function padZero(n) {
    return n < 10 ? '0' + n : n
}



